<?php


namespace app\user\controller;


use think\Controller;
use think\Db;





//爬虫接口
class Pa extends Controller
{

    //根据id获取结果
    public function showResultById($resultId)
    {
        $curl = curl_init();
        curl_setopt_array($curl, array(
            CURLOPT_URL => "https://api.ydl.com/api/test-result/show?resultId=$resultId",
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => "",
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 0,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => "GET",
        ));

        $response = curl_exec($curl);

        curl_close($curl);
        return $response;
    }

    //获取所有的返回结果
    public function getAllReslut()
    {

        //  找到所有跳转类型的题目
        $list = Db::table('test_data')->where([
            'type' => 'jump',
            'answer' => ''
        ])->select();


        for ($i = 0; count($list); $i++) {
            $res = $this->getReslut($list[$i]);

            Db::table('test_data')->where(['id' => $list[$i]['id']])->update([
                'answer' => json_encode(
                    $res,
                    JSON_UNESCAPED_UNICODE
                )
            ]);
        }
    }

    //获取结果
    public function getReslut($test)
    {

//        //数据库的题目id   ,默认题目都是jump
        $test_id = $test['id'];
//
//
//        $test=Db::table('test_data')->where("id",  $test_id)->find( );

        //问题数组
        $list = json_decode($test['data']);
        //结果的数组
        $result = [];

        $result2 = [];

        //遍历问题数组
        for ($i = 0; $i < count($list); $i++) {

            $testAnswerDtos = $list[$i]->testAnswerDtos;

            for ($j = 0; $j < count($testAnswerDtos); $j++) {
                $jumpAnsId = $testAnswerDtos[$j]->jumpAnsId;

                //不为数字不为空的时候，就是指的跳转到结果
                if (!is_numeric($jumpAnsId) && $jumpAnsId != '') {
                    $result[$jumpAnsId] = $testAnswerDtos[$j]->id;
                }
            }
        }


        //获取结果数组
        foreach ($result as $key => $id) {

            $curl = curl_init();
            //获取结果id
            curl_setopt_array($curl, array(
                CURLOPT_URL => "https://api.ydl.com/api/test-result/save",
                CURLOPT_RETURNTRANSFER => true,
                CURLOPT_ENCODING => "",
                CURLOPT_MAXREDIRS => 10,
                CURLOPT_TIMEOUT => 0,
                CURLOPT_FOLLOWLOCATION => true,
                CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                CURLOPT_CUSTOMREQUEST => "POST",
                CURLOPT_POSTFIELDS => "{\"ffrom\":\"m\",\"itemId\":\"$test_id\",\"testAnswerIdList\":[$id]}",
                CURLOPT_HTTPHEADER => array(
                    "Content-Type: application/json"
                ),
            ));

            $response = curl_exec($curl);

            $resultId = json_decode($response)->data->resultId;


            $res2 = $this->showResultById($resultId);

            $data = json_decode($res2)->data;
            $result2[$key] = [
                'name' => $data->name,
                'desc' => $data->desc,
                'resultAnalyse' => $data->resultAnalyse,
                'desc' => $data->desc,
            ];

        }
        return $result2;
    }


    //直接爬取题目列表
    public function index()
    {
        for ($i = 2000; $i < 3000; $i++) {
            try {
                $curl = curl_init();

                curl_setopt_array($curl, array(
                    CURLOPT_URL => "https://api.ydl.com/api/test-item/one-detail?itemId=" . $i,
                    CURLOPT_RETURNTRANSFER => true,
                    CURLOPT_ENCODING => "",
                    CURLOPT_MAXREDIRS => 10,
                    CURLOPT_TIMEOUT => 0,
                    CURLOPT_FOLLOWLOCATION => true,
                    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                    CURLOPT_CUSTOMREQUEST => "GET",
                ));

                $response = curl_exec($curl);

                $data = json_decode($response);

                $questions = $data->data->questions;

                $jumpAnsId = $data->data->questions[0]->testAnswerDtos[0]->jumpAnsId;

                if ($jumpAnsId != '') {
                    $type = 'jump';
                } else if (is_numeric($jumpAnsId)) {
                    $type = 'score';
                }

                Db::table('test_data')->insert([
                    'id' => $data->data->id,
                    'name' => $data->data->name,
                    'tipsTitle' => $data->data->tipsTitle,
                    'desc' => $data->data->desc,
                    "data" => json_encode($questions, JSON_UNESCAPED_UNICODE),
                    "type" => $type,
                    "questionNumber" => $data->data->questionNumber,
                    "hasUnused" => $data->data->hasUnused,
                    "answer" => '',
                ]);
                curl_close($curl);
            } catch (\Exception $e) {

            }
        }

//            $this->result($data->data->name,200,'结果成功');
    }

}
